<?php
require_once 'TVB/pms/pdes/common/util/Unpublished.class.php';
require_once 'TVB/pms/pdes/action/InfoGetAction.class.php';

class InfoSwitchAction extends BaseAction 
{
	public $id;
	public $type;
	public $onair_episode_no;
	public $programme_id;
	public $check_used;
	public $act;

	public $ut;

	public function execute(){
		if (count($this->_errors) > 0)
		{
			return "filter-error";
		}

		switch($this->type)
		{
			case 'address':
				$object = "info_address";
				break;
			case 'recipe':
				$object = "info_recipe";
				break;
			case 'biography':
				$object = "info_biography";
				break;
			case 'product':
				$object = "info_product";
				break;
			case 'qa':
				$object = "info_q_a";
				break;
			case 'award':
				$object = "info_award";
				break;
			case 'general_info':
				$object = "info_general_information";
				break;
			case 'pageant':
				$object = 'info_pageant';
				break;
			case 'song':
				$object = "info_song";
				break;
		}

		if (isset($_REQUEST['delete']))
		{
			return $this->delete_info($object);
		}
		elseif (isset($_REQUEST['recover']))
		{
			return	$this->recover_info($object);
		}
		elseif (isset($_REQUEST['edit']))
		{
			return $this->edit_info($object);
		}
		elseif (isset($_REQUEST['unpublish']))
		{
			return $this->unpublish_info($object);
		}
        elseif (isset($_REQUEST['unpublish_premium']))
		{
			return $this->unpublish_premium_info($object);
		}
	}    

	private function get_info_used_details($object,$id){
		$key = AC::$name2Type[$object];
		$sql = '
			SELECT c.id, c.infos, ev.video_id FROM '.$object .' i 
			INNER JOIN `episode-video` ev ON i.episode_id=ev.episode_id
			INNER JOIN chapter c ON ev.video_id=c.video_id
			WHERE i.id=:id AND c.infos LIKE :key
		';
		$data = array();
		$rs = Model::selectBySql($sql,array(':id'=>$id,':key'=>'%"'.$key.'":%'));
		if($rs) foreach($rs as $row){
			$infos = json_decode($row['infos'],1);
			if(in_array($id, $infos[$key])){
				$data[] = array(
					'chapter_id'=>$row['id'],
					'chapter_infos'=>$infos,
					'video_id'=>$row['video_id'],
				);
			}
		}
		return $data;
	}

	/**
	 * delete info
	 */
	private function delete_info($object)
	{
		$key = AC::$name2Type[$object];
		try
		{
			$used_details = $this->get_info_used_details($object,$this->id);
			if( $this->check_used && $used_details ){
				$this->setData('type',$key);
				$this->setData('act','view');
				$this->setData('id',$this->id);
				$getAction = new InfoGetAction();
				$getAction->id = $this->id;
				$getAction->type = $key;
				$getAction->act = 'view';
				$getAction->execute();

				$old_data = $getAction->getData();
				foreach($old_data as $k=>$v){
					$this->setData($k,$v);
				}
				$this->setData('check_used',0);
				$video_ids = array();
				foreach($used_details as $row){
					$video_ids[] = $row['video_id'];
				}
				$video_ids = array_unique($video_ids);
				$this->addError("{$object}:".AC::LOG_OP_DELETE.":".$this->id, array('0109',"Info $key delete error, because the $key has used in video[".implode(',',$video_ids)."] chapter.(Resubmit should be deleted)", ' Info has used in video chapter.'));
				return 'logic-error';
			}
			if($used_details) foreach($used_details as $chapter){
				$update_sql = 'update chapter set infos=:infos where id=:id';
				$infos = $chapter['chapter_infos'];
				$index = array_search($this->id, $infos[$key]);
				if($index !== false) unset($infos[$key][$index]);
				$ok = Model::updateBySql($update_sql,array(':infos'=>json_encode($infos),':id'=>$chapter['chapter_id']));
			}

			$$object = new $object();
			$info_detail = $$object->get($this->id);
			$info_detail->status = AC::STATUS_DELETED;
			$info_detail->modified_at = date("Y-m-d H:i:s");
			$info_detail->modified_by = CommonUtils::get_user();
			$update_id = Model::$dbo['w']->save($info_detail);
			if ($update_id > 0)
			{
				$info_detail->delete_change_log();
				$this->addInfo("{$object}:".AC::LOG_OP_DELETE.":".$this->id, "{$object} delete success", json_encode($info_detail));
			}
			else
			{
				$this->addError("{$object}:".AC::LOG_OP_DELETE.":".$this->id, "{$object} delete error", json_encode($info_detail));
			}

			$this->setData('type', $this->type);
			$this->setData('programme_id', $this->programme_id);
			$this->setData('onair_episode_no', $this->onair_episode_no);
			return 'delete';
		}
		catch(Exception $e)
		{
			error_log("delete info{$object} : {$this->id}" . var_export($e,1));

			return 'system-error';
		}
	}

	/**
	 * edit info
	 */
	private function edit_info($object)
	{
		try
		{
			$$object = new $object();
			$info_detail = $$object->get($this->id);
			if ($info_detail->status == AC::STATUS_DELETED)
			{
				$this->setData('act', 'view');
				$this->setData('id', $this->id);
				$this->setData('type', $this->type);
				$this->setData('error_code', '0108');
				return 'logic-error';
			}
			$this->setData('id', $this->id);
			$this->setData('type', $this->type);
			$this->setData('act', 'edit');

			return 'edit';
		}
		catch(Exception $e)
		{
			error_log("edit info{$object} : {$this->id}" . var_export($e,1));

			return 'system-error';
		}
	}

	/**
	 * reset info
	 */
	private function reset_info()
	{

		$this->setData('id', $this->id);
		$this->setData('type', $this->type);
		$this->setData('act', $this->act);

		return 'reset';
	}

	/**
	 * recover info
	 */
	private function recover_info($object)
	{
		try
		{
			$$object = new $object();
			$info_detail = $$object->get($this->id);
			$info_detail->status = AC::STATUS_NEW;
			$info_detail->modified_at = date("Y-m-d H:i:s");
			$info_detail->modified_by = CommonUtils::get_user();
			$update_id = Model::$dbo['w']->save($info_detail);
			if ($update_id > 0)
			{
				$info_detail->add_change_log();
				$this->addInfo("{$object}:".AC::LOG_OP_RECOVER.":".$this->id, "{$object} recover success", json_encode($info_detail));
			}
			else
			{
				$this->addError("{$object}:".AC::LOG_OP_RECOVER.":".$this->id, "{$object} recover error", json_encode($info_detail));
			}

			$this->setData('programme_id', $this->programme_id);
			$this->setData('onair_episode_no', $this->onair_episode_no);
			$this->setData('type', $this->type);
			return 'recover';
		}
		catch(Exception $e)
		{
			error_log("recover info{$object} : {$this->id}" . var_export($e,1));

			return 'system-error';
		}
	}

	///** Merge to unpublish_info
    private function unpublish_premium_info($object)
    {
    	try
		{
			$$object = new $object();
			$info_detail = $$object->get($this->id);
			
			$episode = new episode();
			$episode = $episode->get($info_detail->episode_id);
			$log_id  = CommonUtils::save_log($object, $this->id, AC::LOG_OP_UNPUBLISHING, json_encode($info_detail));
			$unpublish_flag = P_Unpublished::send($object, $this->id, $episode->programme_id, $log_id);

			$this->setData('programme_id', $this->programme_id);
			$this->setData('onair_episode_no', $this->onair_episode_no);
			$this->setData('type', $this->type);
			return 'unpublish';
		}
		catch(Exception $e)
		{
			error_log("unpublish info{$object} : {$this->id}" . var_export($e,1));

			return 'system-error';
		}
    }
	// */
	/**
	 * unpublish info
	 */
	private function unpublish_info($object)
	{
		try
		{
			$$object = new $object();
			$info_detail = $$object->get($this->id);
			
			$episode = new episode();
			$episode = $episode->get($info_detail->episode_id);
			$log_id  = CommonUtils::save_log($object, $this->id, AC::LOG_OP_UNPUBLISHING, json_encode($info_detail));
			Unpublished::send($object, $this->id, $episode->programme_id, $log_id);

			// donald 2011-03-21
			//if(CommonUtils::checkLastPublishStatus($object,$this->id,AC::STATUS_PUBLISH_PREMIUM)){
			//	#$log_id  = CommonUtils::save_log($object, $this->id, AC::LOG_OP_PREMIUM_UNPUBLISHING, json_encode($info_detail));
			//	P_Unpublished::send($object, $this->id, $episode->programme_id, $log_id);
			//}

			$this->setData('programme_id', $this->programme_id);
			$this->setData('onair_episode_no', $this->onair_episode_no);
			$this->setData('type', $this->type);
			return 'unpublish';
		}
		catch(Exception $e)
		{
			error_log("unpublish info{$object} : {$this->id}" . var_export($e,1));

			return 'system-error';
		}
	}
}
?>
